<template>
  <div class="min-h content-layout">
    <div class="bg-wt">
      <SwitchBar ref="tabBar" :data="tabData" :route-type="routeType" @change-id="getFloorId"/>
      <HouseList :base-data="roomVoList" :floor-data="tabData" :route-type="routeType" @handleCheck="handleRoomCheck"
                 @handleSelectOlder="handleSelectOlder" @handleSingleOlder="handleSingleOlder"/>
      <Setting ref="formRef" :data="nurseList" :singleNurse="singleNurse" :visible="checkVisible"
               @handle-close="handleCheckRoomClose" @handle-submit="handleSubmit"/>
    </div>
  </div>
</template>
<script setup>
import useUserStore from '@/store/modules/user';
import {getAllUserList} from '@/api/system/user';
import {getRoomBedWithNurList, listAllWithNurFloor, setNursing,} from '@/api/nursing/floor';
import SwitchBar from '@/components/switchBar/switchBartop.vue';
import HouseList from './components/List.vue';
import Setting from './components/Setting.vue';

const {proxy} = getCurrentInstance();
const userStore = useUserStore();
const route = useRoute();
const tabBar = ref(null);
const tabData = ref([]);
const roomVoList = ref([]);
const floorId = ref('');
const floorIndex = ref(null);
const checkVisible = ref(false);
const routeType = ref(null);
const selectOlder = ref([]);
const singleNurse = ref([]);
const formRef = ref(null);
const nurseList = ref([]);

onMounted(() => {
  if (route.query.type !== undefined) {
    routeType.value = route.query.type;
  }
  getAllFloorList();
  getAllUserListFunc();
});

// 获取单个设置老人的护理人数据
const handleSingleOlder = (val) => {
  console.log(val, 1)
  singleNurse.value = val;
};

// 获取要设置的老人数据
const handleSelectOlder = (val) => {
  console.log(val)
  selectOlder.value = val;
};

// 给老人设置护理员
const handleSubmit = async (val) => {
  const params = selectOlder.value.map((item) => {
    return {elderId: item.elderId, nursingIds: val}
  })
  const res = await setNursing(params);
  if (res.code === 200) {
    proxy.$modal.msgSuccess('设置成功');
    getAllFloorList();
    handleCheckRoomClose();
    formRef.value.handleClear();
  } else {
    MessagePlugin.error(res.msg);
  }
};

// 获取护理员下拉数据
const getAllUserListFunc = async () => {
  const res = await getAllUserList({deptId: '201'});
  nurseList.value = res.data;
};

// 获取楼层数据
const getAllFloorList = async () => {
  const res = await listAllWithNurFloor();
  if (res.code === 200) {
    tabData.value = res.data;
    if (floorId.value === '') {
      floorId.value = tabData.value[0].id;
    }
    getList();
  }
};

// 根据不同的楼层id房间信息
const getList = async () => {
  const res = await getRoomBedWithNurList(floorId.value);
  if (res.code === 200) {
    roomVoList.value = res.data;
  }
};

// 获取楼层id
const getFloorId = (id, i) => {
  floorIndex.value = i;
  floorId.value = id;
  getAllFloorList();
  userStore.setFloorInfo({});
};

// 查看房间
const handleRoomCheck = () => {
  // handleRoomData(id)
  checkVisible.value = true;
};

// 关闭查看房间
const handleCheckRoomClose = () => {
  checkVisible.value = false;
  selectOlder.value = [];
  singleNurse.value = [];
};
</script>
<style lang="less" src="./../floor/index.scss"></style>
